package com.zzh.bc.dao.api.impl;

import com.zzh.bc.dao.api.ApiDao;
import com.zzh.bc.db.C3p0Utils;
import com.zzh.bc.db.MySQLExecute;
import com.zzh.bc.db.RSUtils;
import com.zzh.bc.model.ApiModel;
import com.zzh.bc.model.ApiParamModel;
import com.zzh.bc.model.PageModel;
import com.zzh.bc.model.ProjectModel;

import java.sql.SQLException;
import java.util.List;

/**
 * 接口参数。
 */
public class ApiDaoImpl implements ApiDao {

    @Override
    public ApiModel findObjById(String id) {
        String sql = "SELECT " + ApiModel.SQL_COLUMN_NAME + " FROM " + ApiModel.TABLE_NAME + " WHERE api_id = ?";
        String subSQL = "SELECT " + ApiParamModel.SQL_COLUMN_NAME + " FROM " + ApiParamModel.TABLE_NAME + " WHERE api_project_id = ?";
        ApiModel objects = null;

        try {
            objects = RSUtils.cast2Bean(MySQLExecute.executeQuery(sql, id), ApiModel.class);
            List<ApiParamModel> subApiParams = RSUtils.cast2ListBean(MySQLExecute.executeQuery(subSQL, id), ApiParamModel.class);
            if (objects != null) {
                objects.setData(subApiParams);
            }
        } catch (IllegalAccessException | InstantiationException | SQLException e) {
            e.printStackTrace();
        }
        return objects;
    }

    @Override
    public List<ApiModel> findAllObjByPage(PageModel pageModel) {
        return null;
    }

    /**
     * 保存数据.
     *
     * @param model 数据实体类
     * @return 插入成功返回ture
     */
    @Override
    public boolean save(ApiModel model) {
        String sql = "INSERT INTO " + ApiModel.TABLE_NAME + " (" + ApiModel.SQL_COLUMN_NAME + ") VALUES (" + ApiModel.SQL_COLUMN_PARAMS + ")";
        try {
            boolean flagSaveApiModel = MySQLExecute.insert(sql, model.getApiId(), model.getApiName(), model.getApiPattern(),
                    model.getApiProjectId(), model.getApiReqMethod(),
                    model.getCreatedBy(), model.getCreatedTime(), model.getUpdatedBy(), model.getUpdatedTime()) == 1;
            return flagSaveApiModel;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(ApiModel model) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(ApiModel model) {
        return false;
    }

    @Override
    public void beginTransaction() {
        try {
            C3p0Utils.beginTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void commitTransaction() {
        try {
            C3p0Utils.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void rollbackTransaction() {
        try {
            C3p0Utils.rollbackTransaction();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public boolean delete(String apiId) {
        boolean delete = false;
        try {
            delete = MySQLExecute.delete(ApiModel.SQL_DELETE, apiId)==1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return delete;
    }
}
